package com.example.androidtraining_ahut2020;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivityDay09Calculator extends AppCompatActivity {
    EditText editTextA, editTextB;
    Button btnEqual;
    TextView textViewC;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_day09_caculator);
        editTextA = findViewById(R.id.editTextA);
        editTextB = findViewById(R.id.editTextB);
        btnEqual = findViewById(R.id.btnEqual);
        textViewC = findViewById(R.id.textViewC);
        btnEqual.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int a = 0;
                int b = 0;
                try {
                    a = Integer.parseInt(editTextA.getText().toString());
                    b = Integer.parseInt(editTextB.getText().toString());

                } catch (NumberFormatException e) {
                    Toast.makeText(MainActivityDay09Calculator.this, "计算错误", Toast.LENGTH_SHORT).show();
                }
                textViewC.setText(String.format("%d", a + b));
            }
        });

    }
}